#include "jsapi.h"
#include "js-engine.h"
#include "desktopClasses.h"
#include "basicElementClass.h"
#include "mainview-parser.h"

static JSBool radio_new(JSContext *cx, JSObject *obj, uintN argc, jsval *args, jsval *rval);

struct JSFunctionSpec radio_methods[] = {
    {NULL,NULL}
};

JSClass radioClass =
    { "radio", JSCLASS_HAS_PRIVATE,
      JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
      JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
      JSCLASS_NO_OPTIONAL_MEMBERS
    };

struct property_parser radio_parser[] = {
    /* from basicElement */
    {"cursor", set_property_string_value},
    {"dropTarget", set_property_boolean_value},
    {"enabled", set_property_boolean_value},
    {"height", set_property_integer_value},
    {"hitTest", set_property_string_value},
    {"mask", set_property_string_value},
    {"name", set_property_string_value},
    {"offsetHeight", set_property_integer_value},
    {"offsetWidth", set_property_integer_value},
    {"offsetX", set_property_integer_value},
    {"offsetY", set_property_integer_value},
    {"opacity", set_property_integer_value},
    {"pinX", set_property_integer_value},
    {"pinY", set_property_integer_value},
    {"rotation", set_property_float_value},
    {"tagName", set_property_string_value},
    {"tooltip", set_property_string_value},
    {"width", set_property_integer_value},
    {"visible", set_property_boolean_value},
    {"x", set_property_integer_value},
    {"y", set_property_integer_value},
		
    {ONCLICK, set_property_function_value},
    {ONRCLICK, set_property_function_value},
    {ONDBLCLICK, set_property_function_value},
    {ONRDBLCLICK, set_property_function_value},
    {"ondragdrop", set_property_function_value},
    {"ondragover", set_property_function_value},
    {"onfocusin", set_property_function_value},
    {"onfocusout", set_property_function_value},
    {"onkeydown", set_property_function_value},
    {"onkeypress", set_property_function_value},
    {"onkeyup", set_property_function_value},
    {"onmousedown", set_property_function_value},
    {"onmousemove", set_property_function_value},
    {"onmouseout", set_property_function_value},
    {"onmouseover", set_property_function_value},
    {"onmouseup", set_property_function_value},
    {"onmousewheel", set_property_function_value},		
    {NULL, NULL},
};

static JSBool radio_new(JSContext *cx, JSObject *obj, uintN argc, jsval *args, jsval *rval)
{
    return JS_TRUE;
}

JSObject *radioClass_init(js_engine *engine, JSObject *parent)
{
    JSObject *proto = NULL;
    proto = JS_InitClass(engine->cx, engine->glob, parent,
			 &radioClass, radio_new,
			 0,NULL,NULL,NULL,NULL);
    g_return_val_if_fail(proto, FALSE);
    g_return_val_if_fail(JS_DefineFunctions(engine->cx, proto, radio_methods), FALSE);
    return proto;
}

static gboolean radio_button_press_cb(GtkWidget *widget, GdkEventButton *event, gpointer ptr)
{
    desktop_element *element = (desktop_element *)ptr;
    switch (event->button){
    case 1:{
	switch(event->type){
	case GDK_BUTTON_PRESS:
	    desktop_element_call_handler(element, ONCLICK);
	    break;
	case GDK_2BUTTON_PRESS:
	    desktop_element_call_handler(element, ONDBLCLICK);
	    break;
	default:
	    break;
	}
	break;
    }
    case 3:{
	switch(event->type){
	case GDK_BUTTON_PRESS:
	    desktop_element_call_handler(element, ONRCLICK);
	    break;
	case GDK_2BUTTON_PRESS:
	    desktop_element_call_handler(element, ONRDBLCLICK);
	    break;
	default:
	    break;
	}
	break;
    }
    }
    return FALSE;
}

void desktop_element_set_view_radio(js_engine *engine, desktop_element *element)
{
    desktop_element *root_view = JS_GetPrivate(engine->cx, engine->view);
    GSList *radio_group =(GSList *)desktop_element_get_private(root_view);
    jsval rval;
    JS_GetProperty(engine->cx, element->jsobj, "caption", &rval);
    if(JSVAL_IS_STRING(rval)){
	gchar *caption = JS_GetStringBytes(JSVAL_TO_STRING(rval));
	element->view = gtk_radio_button_new_with_label(radio_group, caption);
    }else{
	element->view = gtk_radio_button_new(radio_group);
    }
    set_element_event_cb(element);
    desktop_element_set_private(root_view, (gpointer)gtk_radio_button_get_group(GTK_RADIO_BUTTON(element->view)));
}
